﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Setting_do_LoadDept : SessionPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string id = "0";
        if (!string.IsNullOrEmpty(Request.QueryString["id"]))
        {
            id = Request.QueryString["id"];
        }
        string dataString = ListSubDept(id);

        Response.Write(dataString);
    }

    public string ListSubDept(string parentID)
    {
        #region dynaTree
        /**
			 * res = '[' 
			 * res += '{ "title": "Node 1", "key": "k1", "isLazy": true },' 
			 * res += '{ "title": "Node 2", "key": "k2", "isLazy": true },' 
			 * res += '{ "title": "Node 3", "key": "k3", "isLazy": true }' # no trailing "," at the last line
			 * res += ']'
			 */
        try
        {
            string result = "";

            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            LogicLayer mgr = new LogicLayer();

            List<SYS_Department> lstDept = mgr.ListDeptByParent(parentID);

            if (lstDept == null)
                return "error";

            sb.Append("[");

            foreach (SYS_Department dept in lstDept)
            {
                sb.Append("{ \"title\": \"" + dept.DeptName + "\", \"key\": \"" + dept.DeptID + "\", \"isFolder\" : \"true\" ");
                if (mgr.HasChildDept(dept.DeptID))
                    sb.Append(",\"isLazy\": true ");

                sb.Append("},");
            }
            result = sb.ToString();
            if (result.EndsWith(","))
                result = result.Substring(0, result.Length - 1);

            result += "]";

            if (parentID == "0")
            {
                result = "[ { \"title\": \"所有部门\", \"key\": \"0\", \"isFolder\": true, \"isLazy\": true , \"expand\":true, \"children\": " + result + "}]";
            }

            return result;  // 输出JSON字符串
        }
        catch //(Exception ex)
        {
            //Logger.Write(ex.Message);
            return "error";
        }
        #endregion
    }
}